package com.banciyuan.bcywebview.biz.main.mineinfo.mark;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.CircleApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.main.mainpage.maincircle.MainCircleAdapter;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.MainCircle;

public class MyAcgFragment extends OtherAcgFragment {
    private List<MainCircle> mMainCircleList = new ArrayList();
    private MainCircleAdapter mMainCircleAdapter;

    @Override
    protected void initData() {
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(getActivity()).getToken()));
        if (!TextUtils.isEmpty(uid)) {
            paramsList.add(new BcyPair(HttpUtils.UID, uid));
        }
        paramsList.add(new BcyPair(HttpUtils.INDEX_P, page + ""));
        paramsList.add(new BcyPair(HttpUtils.LIMIT, PAGE_LIMIT + ""));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + CircleApi.getFocusedcircle();

        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, getActivity())) {
                    try {
                        JSONObject result = new JSONObject(s);
                        JSONObject dataObject = result.getJSONObject(HttpUtils.UPLOAD_DATA);
                        Gson gson = new Gson();
                        List<MainCircle> tempList = gson.fromJson(dataObject.getString("core_data"), new TypeToken<List<MainCircle>>() {
                        }.getType());
                        processEmptyView(tempList);
                        processFooterView(tempList);
                        processData(tempList);
                    } catch (Exception e) {
                        loading = false;
                        mProgressbarHelper.onFailed();
                        refreshView.onRefreshComplete();
                    }
                } else {
                    loading = false;
                    mProgressbarHelper.onFailed();
                    refreshView.onRefreshComplete();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                loading = false;
                refreshView.onRefreshComplete();
                if (page == 1) {
                    mProgressbarHelper.onFailed();
                    emptyView.setVisibility(View.GONE);
                } else {
                    page -= 1;
                }
            }
        };

        OnErrorProxy errorProxy = new OnErrorProxy(error, succ, url, getActivity(), params);
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, errorProxy);
        queue.add(stringRequest);
    }

    private void processFooterView(List<MainCircle> tempList) {
        if (tempList.size() < PAGE_LIMIT)
            footerView.setVisibility(View.VISIBLE);
    }

    private void processEmptyView(List<MainCircle> tempList) {
        if (tempList.size() == 0) {
            if (page == 1) {
                emptyView.setVisibility(View.VISIBLE);
            }
            hasMore = false;
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }

    private void processData(List<MainCircle> tempList) {
        loading = false;
        if (page == 1) {
            mMainCircleList.clear();
        }
        mMainCircleList.addAll(tempList);
        if (null == mMainCircleAdapter) {
            mMainCircleAdapter = new MainCircleAdapter(getActivity(), mMainCircleList);
            mListView.setAdapter(mMainCircleAdapter);
        } else {
            mMainCircleAdapter.notifyDataSetChanged();
        }
        mProgressbarHelper.onSuccess();
        loading = false;
        refreshView.onRefreshComplete();
    }

    protected void onItemClicked(int i) {
        int postion = i - 1;
        if (postion < 0 || postion >= mMainCircleList.size()) return;

        Intent intent = new Intent();
        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, mMainCircleList.get(postion).getName());
        intent.putExtra(gotoUtil.INTENT_VALUE_TWO, mMainCircleList.get(postion).getId());
        intent.putExtra("from", 1);
        intent.setClass(getActivity(), CircleSmoothActivity.class);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != mMainCircleAdapter) {
            mMainCircleAdapter.updateView(mListView);
        }
    }
}
